package com.ia.uniweb.userfacing;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ia.uniweb.cass.CassandraProxy;
import com.ia.uniweb.model.Sex;
import com.ia.uniweb.model.Student;
import com.ia.uniweb.service.StudentService;
import com.ia.uniweb.service.StudentServiceImpl;

public class ProfileLet extends HttpServlet {

	private StudentService studentService;

	private CassandraProxy cassandra;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);

		// Initialise cassandra
		cassandra = CassandraProxy.initialise();

		studentService = new StudentServiceImpl(
				cassandra.getConnectedKeyspace());

		System.out.println("Initialised servlet and cassandra");
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String requestType = request.getParameter("requestType");

		if (requestType != null && !requestType.isEmpty()) {

			if (requestType.equals("search")) {
				searchMember(request, response);
			} else if (requestType.equals("create")) {
				createMember(request, response);
			} else if (requestType.equals("update")) {
				updateMember(request, response);
			} else {
				// NoOp
			}

		} else {

			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/views/display/unknown.jsp");
			if (dispatcher != null) {
				dispatcher.forward(request, response);
			}

		}

	}

	private void updateMember(HttpServletRequest request,
			HttpServletResponse response) {

	}

	private void createMember(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// If there is a search parameter in the request,
		// lookup and show profile accordingly

		Student student = null;

		String studentQueryFirstName = (String) request
				.getParameter("create-student-firstName");
		String studentQueryLastName = (String) request
				.getParameter("create-student-lastName");
		String studentQueryRegNum = (String) request
				.getParameter("create-student-regNum");
		String studentQueryDob = (String) request
				.getParameter("create-student-dob");

		student = new Student();
		student.setActive(true);
		student.setCourse("IT");
		student.setDobDDMMYY(studentQueryDob);
		student.setFirstName(studentQueryFirstName);
		student.setLastName(studentQueryLastName);
		student.setRegistrationNumber(studentQueryRegNum);
		//TODO Find out how to persist custom classes ?? - SuperColumn ??? 
		//student.setSex(Sex.MALE);
		
		// Persist in cassandra
		studentService.save(student);

		// Query cassandra
		student = studentService.find(studentQueryRegNum);
		request.setAttribute("student-result", student);

		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/views/display/profile.jsp");
		if (dispatcher != null) {
			dispatcher.forward(request, response);
		}
	}

	private void searchMember(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// If there is a search parameter in the request,
		// lookup and show profile accordingly

		String studentQuery = (String) request.getParameter("search-student");
		Student student = null;
		if (studentQuery != null) {

			// Query cassandra
			student = studentService.find(studentQuery);
			request.setAttribute("student-result", student);
		}

		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/views/display/profile.jsp");
		if (dispatcher != null) {
			dispatcher.forward(request, response);
		}
	}

}
